package com.volkanic.views;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.volkanic.controller.SharedController;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Lucas on 22/07/2014.
 */
public class SharedView implements Screen {

    /**
     * The libGdx Game
     */
    protected Game _game;

    /**
     * The controller of the game
     */
    protected SharedController _controller;

    /**
     * The batch to draw the view
     */
    protected SpriteBatch _batch;

    /**
     * The list of different stage of the screens to draw
     */
    protected List<Stage> _listStages;

    /**
     * The current stage of the game
     */
    protected SharedController.StageState _currentState;

    /**
     * Create a new instance of the class SharedView
     * @param game the libGdx game
     * @param state the current state of the stage
     */
    public SharedView(Game game, SharedController controller, SharedController.StageState state){
        _game = game;
        _currentState = state;
        _controller = controller;
    }

    /**
     * Render the scene of stages to the screen
     * @param delta the delta of time between to rendering
     */
    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // does the acting work
        this.stagesAct();

        // does the drawing work
        _batch.begin();
        this.stagesDraw();
        _batch.end();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {
        _game.pause();
    }

    @Override
    public void resume() {
        _game.resume();
    }

    @Override
    public void dispose() {
        Iterator<Stage> iterator = _listStages.iterator();
        while(iterator.hasNext()){
            iterator.next().clear();
        }
    }

    /**
     * Act each stage of the screen
     */
    private void stagesAct() {
        Iterator<Stage> iteratorStages = _listStages.iterator();
        while (iteratorStages.hasNext()){

            iteratorStages.next().act();
        }
    }

    /**
     * Draw each stage stage on the screen
     */
    private void stagesDraw() {
        Iterator<Stage> iteratorStages = _listStages.iterator();
        while (iteratorStages.hasNext()) {
            iteratorStages.next().draw();
        }
    }
}
